#define _CRT_SECURE_NO_WARNINGS 1

#include <unordered_map>
using namespace std;

class Solution {
public:
    bool containsNearbyDuplicate(vector<int>& nums, int k) {
        unordered_map<int, int> hash;
        int n = nums.size();
        for (int i = 0; i < n; ++i)
        {
            if (hash.count(nums[i]) && i - hash[nums[i]] <= k) return true;
            else hash[nums[i]] = i;
        }
        return false;
    }
};